// 修复方案：
// 1. 将 toString 转换为安全的函数包装
// 2. 修复 isFunction 的类型检查
// 3. 简化正则表达式，移除无用转义字符

// 修复 1: 将直接引用改为安全函数包装
const toTypeString = (value: any) => Object.prototype.toString.call(value);

const fileType: any = {
    video: [
        '.mp4',
        '.avi',
        '.flv',
        '.mov',
        '.wmv',
        '.mkv',
        '.webm',
        '.mpeg',
        '.mpg',
    ],
    image: [
        '.jpg',
        '.jpeg',
        '.png',
        '.gif',
        '.bmp',
        '.svg',
        '.webp',
        '.tiff',
        '.tif',
    ],
};

export const is = (val: unknown, type: string) => {
    return toTypeString(val) === `[object ${type}]`;
};

export const isDef = <T = unknown>(val?: T): val is T => {
    return typeof val !== 'undefined';
};

export const isUnDef = <T = unknown>(val?: T): val is T => {
    return !isDef(val);
};

export const isObject = (val: any): val is Record<any, any> => {
    return val !== null && is(val, 'Object');
};

export const isEmpty = <T = unknown>(val: T): val is T => {
    if (isArray(val) || isString(val)) {
        return val.length === 0;
    }

    if (val instanceof Map || val instanceof Set) {
        return val.size === 0;
    }

    if (isObject(val)) {
        return Object.keys(val).length === 0;
    }

    return false;
};

export const isDate = (val: unknown): val is Date => {
    return is(val, 'Date');
};

export const isNull = (val: unknown): val is null => {
    return val === null;
};

export const isNullAndUnDef = (val: unknown): val is null | undefined => {
    return isUnDef(val) && isNull(val);
};

export const isNullOrUnDef = (val: unknown): val is null | undefined => {
    return isUnDef(val) || isNull(val);
};

export const isNumber = (val: unknown): val is number => {
    return is(val, 'Number');
};

export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
    return (
        is(val, 'Promise') &&
        isObject(val) &&
        isFunction(val.then) &&
        isFunction(val.catch)
    );
};

export const isString = (val: unknown): val is string => {
    return is(val, 'String');
};

// 修复 2: 使用更安全的函数类型定义
export const isFunction = (val: unknown): val is (...args: any[]) => any => {
    return typeof val === 'function';
};

export const isBoolean = (val: unknown): val is boolean => {
    return is(val, 'Boolean');
};

export const isRegExp = (val: unknown): val is RegExp => {
    return is(val, 'RegExp');
};

export const isArray = (val: any): val is Array<any> => {
    return val && Array.isArray(val);
};

export const isWindow = (val: any): val is Window => {
    return typeof window !== 'undefined' && is(val, 'Window');
};

export const isElement = (val: unknown): val is Element => {
    return isObject(val) && !!val.tagName;
};

export const isMap = (val: unknown): val is Map<any, any> => {
    return is(val, 'Map');
};

export const isServer = typeof window === 'undefined';

export const isClient = !isServer;

// 修复 3: 简化正则表达式并移除无用转义
export const isUrl = (path: string): boolean => {
    // 使用更可靠的 URL 检测方法
    try {
        // 临时创建一个带有基础域名的完整 URL
        const url = new URL(path, 'http://example.com');

        // 确保协议是有效的
        const validProtocols = ['http:', 'https:'];
        return validProtocols.includes(url.protocol);
    } catch (e) {
        return false;
    }
};

export const isDark = (): boolean => {
    return false;
};

export const isVideoUrl = (value: any) => {
    return isFile(value, 'video');
};

export const isImageUrl = (value: any) => {
    return isFile(value, 'image');
};

export const isFile = (value: any, type: any) => {
    const videoExtensions = fileType[type];
    const lowerUrl = value.toLowerCase();

    return videoExtensions.some((extension: any) =>
        lowerUrl.endsWith(extension)
    );
};